package com.itheima;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.BookMapper;
import com.itheima.pojo.Book;
import com.itheima.pojo.MyPage;
import com.itheima.pojo.MyQw;
import com.itheima.service.IBookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class Springboot06SsmpApplicationTests2 {

	@Autowired
	private IBookService iBookService;

	//增加
	@Test
	void testAdd() {
		boolean flag = iBookService.save(new Book(null,"山村老尸","恐怖",350));
		if (!flag){
			System.out.println("添加失败!");
		} else {
			System.out.println("添加成功!");
		}
	}

	//修改
	@Test
	void testUpdate() {
		boolean flag = iBookService.updateById(new Book(7,"山村老尸","恐怖",350));
		if (!flag){
			System.out.println("修改失败!");
		} else {
			System.out.println("修改成功!");
		}
	}

	//删除
	@Test
	void testDelete() {
		boolean flag = iBookService.removeById(7);
		if (!flag){
			System.out.println("删除失败!");
		} else {
			System.out.println("删除成功!");
		}
	}

	//查一个
	@Test
	void testGetById() {
		Book b = iBookService.getById(1);
		System.out.println(b);
	}

	//查全部
	@Test
	void testGetAll() {
		List<Book> list = iBookService.list();
		System.out.println(list);
	}

	//分页查询
	@Test
	void testGetPage() {
		IPage<Book> page = new Page<>(1,5);
		iBookService.page(page);
		System.out.println(page.getRecords());
	}

	//多条件查询
	@Test
	void testWrapper() {
		LambdaQueryWrapper<Book> qw = new LambdaQueryWrapper<>();
		qw.like(Book::getType,"恐怖");
		List<Book> list = iBookService.list(qw);
		System.out.println(list);
	}


	//多条件查询
	@Test
	void testInitList() {
		List<Book> books = iBookService.initList(1,5,"","1");
		books.forEach(System.out::println);
	}

	//多条件查询
	@Test
	void testInitList2() {
		IPage<Book> page = iBookService.initListPage(1, 5, "", "");
		System.out.println(page.getTotal());
		System.out.println(page.getCurrent());
		System.out.println(page.getSize());
		System.out.println(page.getPages());
		System.out.println(page.getRecords());
	}


	//多条件查询
	@Test
	void testTotal() {
		int count = iBookService.count();
		System.out.println(count);
	}

}
